﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//
namespace DEMO_TEMP1.Store_Cargo
{
    public partial class MAWBSelect : Form
    {
        WebPor.Service1 lc = new WebPor.Service1();
        DataSet ds = new DataSet();
        string MAWB,userid = string.Empty;
        string time, HAWB, ttl, st, bal, mawb_no;
        int indexselectmawb;
        bool check;
        public MAWBSelect(string user,string mawb)
        {
            lc.Url = String.Format("http://192.168.1.220/WebAppPor01/Service1.asmx");
            InitializeComponent();
            this.MAWB = mawb;
            this.ds = lc.Getdata(MAWB); ;
            this.userid = user;
            
            if (ds==null)
            {
                MessageBox.Show("DATA ERROR");
            }
            else
            {
                DataTable MAWB_TB = CreateMAWB_TB(ds);
                dataGrid1.DataSource = MAWB_TB;
            }
        }
         
        private DataTable CreateMAWB_TB(DataSet ds)
        {
            
            lc.Url = string.Format("http://192.168.1.220/WebAppPor01/Service1.asmx");
            DataTable TW = new DataTable();
            //DateTime time;
          
            ///////////create head column/////////////////
            TW.Columns.Add("Time", typeof(DateTime));
            TW.Columns.Add("HAWB", typeof(string));
            TW.Columns.Add("Total Piece", typeof(string));
            TW.Columns.Add("Stored Piece", typeof(string));
            TW.Columns.Add("Balance", typeof(string));
            ///////////////////////////////////////////////

            /////////Getdata for table////////////////////
            int row = lc.GetcountMAWB(MAWB);
            for (int i = 0; i < row; i++)
            {
                time = ds.Tables[0].Rows[i]["create_date"].ToString().Trim();
                //time = DateTime.Parse(t);
                HAWB = ds.Tables[0].Rows[i]["hawb"].ToString().Trim();
                ttl = ds.Tables[0].Rows[i]["total_piece_count"].ToString().Trim();
                st = ds.Tables[0].Rows[i]["stored_piece_count"].ToString().Trim();
                bal = CalcuBalinMawbList(ttl, st);
                //bal = ds.Tables[0].Rows[i]["bal"].ToString().Trim();
                TW.Rows.Add(time, HAWB, ttl, st, bal);
            }
            
          

            //string timef = ds.Tables[0].Rows[0]["flight_date"].ToString().Trim();
           // DateTime dt = DateTime.Parse(timef);
           // t = String.Format("{0:dd}", dt);
            //Flt = ds.Tables[0].Rows[0]["carr"].ToString().Trim() + ds.Tables[0].Rows[0]["flight_no"].ToString().Trim() + '/' + t;

            



            
            /////////////////////////////////////////////
            return TW;
        }

        private string CalcuBalinMawbList(string ttl, string st)
        {
            int t = Convert.ToInt32(ttl);
            int s = Convert.ToInt32(st);
            int b = t - s;
           return  Convert.ToString(b);
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            ScanMAWB sm = new ScanMAWB(userid);
            sm.Show();
            this.Close();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            
            lc.Url = string.Format("http://192.168.1.220/WebAppPor01/Service1.asmx");
            int row = lc.GetcountMAWB(MAWB);
            //dataGrid1.Click
            string mawb_no = selectMAWB(indexselectmawb);
            check = RowisSelected(indexselectmawb);
            if(check == true)
            {
                Scanlocation sl = new Scanlocation(userid, MAWB, indexselectmawb);
                sl.Show();
            }
            else
            {
                label2.Visible = true;
                label2.Text = "Row Not Selected";
            }
        }
        private bool RowisSelected(int index)
        {
            bool message;
            if (index < 0)
            {
                message = false;
            }
            else
            {
                message = true;
            }
            return message;
        }
        private string selectMAWB(int m)
        {
            string mawb = ds.Tables[0].Rows[m]["display_mawb"].ToString().Trim();
            return mawb;
        }
        private void dataGrid1_Click(object sender, EventArgs e)
        {
           dataGrid1.Select(dataGrid1.CurrentRowIndex);
           string m = string.Empty;
           m= dataGrid1.CurrentRowIndex.ToString();
           indexselectmawb = int.Parse(m);
           // selectMAWB(m2);
          // MessageBox.Show(m);
            
        }

        private void dataGrid1_DoubleClick(object sender, EventArgs e)
        {
            dataGrid1.UnSelect(dataGrid1.CurrentRowIndex);
        }

       
      
    }
}